package com.banshuibao.bsb_android.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.model.ExpertTreeModel;
import com.banshuibao.bsb_android.util.XToast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangpan on 2018/3/28.
 */

public class MenuTagAdapter extends RecyclerView.Adapter<MenuTagAdapter.ViewHolder> implements View.OnClickListener {
    List<ExpertTreeModel.DataBean.ChildrenBean> list;
    Context context;
    TagItemOnClickListener listener;

    public MenuTagAdapter(Context context) {
        this.context = context;
        list = new ArrayList<>();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.menu_tag_layout,null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.tag_tv.setText(list.get(position).getName());
        holder.itemView.setTag(position);
    }

    public void setData(List<ExpertTreeModel.DataBean.ChildrenBean> list) {
        if (list == null) {
            XToast.showToast(context, "空值");
            return;
        }
        if (this.list != null) {
            this.list.clear();
            this.list.addAll(list);
            notifyDataSetChanged();
        }
    }

    @Override
    public int getItemCount() {
        return list!=null?list.size():0;
    }

    @Override
    public void onClick(View v) {
        if(listener!=null){
            listener.setData(list.get(v.getId()));
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView tag_tv;
        public ViewHolder(View itemView) {
            super(itemView);
            tag_tv = itemView.findViewById(R.id.text_tag);
            tag_tv.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if(listener!=null){
                int position = (int) itemView.getTag();
                listener.setData(list.get(position));
            }
        }
    }

    public interface TagItemOnClickListener{
      void setData(ExpertTreeModel.DataBean.ChildrenBean childrenBean);
    }

    public void setTagItemOnClickListener(TagItemOnClickListener listener){
        this.listener = listener;
    }
}
